<?php

namespace addons\shopro\model;

use think\Model;
use addons\shopro\exception\Exception;

/**
 * 模板装修
 */
class Decorate extends Model
{

    // 表名,不含前缀
    protected $name = 'shopro_decorate';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    protected $hidden = ['createtime', 'updatetime', 'deletetime'];
    // 追加属性
    protected $append = [

    ];

    public static function getCurrentPaltformDecorateId($platform = '', $id = 0)
    {
        $where = ['deletetime' => null, 'status' => 'normal'];

        if ($id !== 0) {
            $where['id'] = $id;
        }


        if ($platform === 'preview') {
            $decorate = self::where($where)->order('id desc')->with(['decorateContent' => function ($query) {
                $query->order('id asc');
            }])->find();
        }else{
            $decorate = self::where('find_in_set(:platform,platform)', ['platform' => $platform])->where($where)->order('id desc')->with(['decorateContent' => function ($query) {
                $query->order('id asc');
            }])->find();
        }
        return $decorate;
    }
    //获取当前平台模板装修数据
    public static function getCurrentPlatformDecorate($platform = '', $id = 0)
    {
        $where = ['deletetime' => null, 'status' => 'normal'];

        if ($id !== 0) {
            $where['id'] = $id;
        }


        if ($platform === 'preview') {
            $decorate = self::where($where)->order('id asc')->with(['decorateContent' => function ($query) {
                $query->order('id asc');
            }])->find();
        }else{
            $decorate = self::where('find_in_set(:platform,platform)', ['platform' => $platform])->where($where)->order('id asc')->with(['decorateContent' => function ($query) {
                $query->order('id asc');
            }])->find();
        }

        if (!$decorate) {
            throw new Exception('未找到模板');
        }

        $template = $decorate->decorate_content;
        $list = [];
        if (!empty($template)) {
            foreach ($template as $k => $templateValue) {
                $list[$k] = [
                    'name' => $templateValue->name,
                    'type' => $templateValue['type'],  //model->type有坑
                    'content' => self::getDecorateContent($templateValue)
                ];
            }
        }
//        halt($list);
        return $list;
    }

    public static function asyncBannerBgColor($platform, $params)
    {
        $decorate = self::getCurrentPaltformDecorateId($platform);
        $content = \addons\shopro\model\DecorateContent::get([
            'decorate_id' => $decorate->id,
            'type' => 'banner'
        ]);
        if ($content) {
            $bannerContent = json_decode($content->content, true);

            $bannerContent['list'][$params['index']]['bgcolor'] =  $params['item']['bgcolor'];
            $content->content = json_encode($bannerContent);
            $content->save();

        }
    }

    public static function getDecorateContent($templateValue)
    {
        $content = json_decode($templateValue->content, true);
        switch ($templateValue['type']) {
            case 'search':
                return $content;
                break;
            case 'banner':
                foreach ($content['list'] as &$l) {
                    $l['image'] = cdnurl($l['image'], true);
                }
                return $content;
                break;
            case 'menu':
                foreach ($content['list'] as &$l) {
                    $l['image'] = cdnurl($l['image'], true);
                }
                return $content;
                break;
            case 'adv':
                foreach ($content['list'] as &$l) {
                    $l['image'] = cdnurl($l['image'], true);
                }
                return $content;
                break;
            case 'goods-list':
                $content['image'] = cdnurl($content['image'], true);
//                $content['data'] = Goods::getGoodsList(['goods_ids' => $content['ids']]);
                return $content;
                break;
            case 'goods-group':
                $content['image'] = cdnurl($content['image'], true);
//                $content['data'] = Goods::getGoodsList(['category_id' => $content['id']]);
                return $content;
                break;
            case 'coupons':
                return $content;
                break;
            case 'seckill':
                return $content;
                break;
            case 'live':
                return $content;
                break;



        }
    }

    public function decorateContent()
    {
        return $this->hasMany(\addons\shopro\model\DecorateContent::class, 'decorate_id', 'id');

    }


}
